package com.taokatao.cardmee;

import android.app.Activity;
import android.app.Dialog;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.taokatao.cardmee.api.model.CardShareInfo;
import com.taokatao.cardmee.business.MainActivity;
import com.taokatao.cardmee.common.widget.ShareDialog;
import com.taokatao.cardmee.R;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 浏览器嵌套活动
 */
public class WebActivity extends BaseActivity implements View.OnClickListener, WebLayout.Callback {
    @BindView(R.id.v_fit) View vFit;
    @BindView(R.id.i_menu) ImageView iMenu;
    @BindView(R.id.t_menu) TextView tMenu;
    @BindView(R.id.i_back) ImageView iBack;
    @BindView(R.id.t_title) TextView tTitle;
    @BindView(R.id.t_close) TextView tClose;
    @BindView(R.id.l_web) WebLayout lWeb;

    private final boolean isSupportClose = true;
    private boolean isFixedTitle;

    private boolean isForceBackFinish = false;

    private String mRightMenuAction;

    private ShareDialog dlgShare;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isSupportTouchBack = false;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        ButterKnife.bind(this);

        isFixedTitle = getIntent().getBooleanExtra("extra_is_fixed_title", false);
        // isSupportClose = getIntent().getBooleanExtra("extra_is_support_close", true);
        // isSupportClose = true;

        lWeb.setCallback(this);
        iMenu.setVisibility(View.GONE);
        tMenu.setVisibility(View.GONE);

        setWebTitle(getIntent().getStringExtra("extra_title"));
        lWeb.loadUrl(getIntent().getStringExtra("extra_url"));

        Utils.fitSysWindowAndSetStatusBarMode(0xFFFFFFFF, vFit);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            iBack.setBackgroundResource(R.drawable.ripple_back_black);
            tClose.setBackgroundResource(R.drawable.ripple_back_black);
            iMenu.setBackgroundResource(R.drawable.ripple_back_black);
            tMenu.setBackgroundResource(R.drawable.ripple_back_black);
        }
    }

    @Override
    @OnClick({R.id.i_back, R.id.t_close, R.id.i_menu, R.id.t_menu})
    public void onClick(View v) {
        if (v == iBack) {
            if (isForceBackFinish) {
                finish();
            } else {
                onBackPressed();
            }
        } else if (v == tClose) {
            finish();
        } else if (v == iMenu) {
            if (TextUtils.equals("returnToHomeCard", mRightMenuAction)) {
                Utils.postMainThreadBusEvent(BusEvent.WEB_RETURN_TO_HOME_CARD);
                ActivityCollector.finishAllExcept(MainActivity.class);
            }
        } else if (v == tMenu){
            if (TextUtils.equals("share", mRightMenuAction)) {
                showShareDialog();
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (!lWeb.webGoBack()) {
            super.onBackPressed();
        }
    }

    @Override
    public void onPageStarted(boolean isRootPage) {
        isForceBackFinish = false;

        if (!isFixedTitle) {
            Utils.loge("WebActivity onPageStarted setWebTitle %s");
            setWebTitle(lWeb.webView.getTitle());
        }
        if (isSupportClose) {
            tClose.setVisibility(isRootPage ? View.GONE : View.VISIBLE);
        }
    }

    @Override
    public void onPageFinished(boolean isRootPage) {
        if (!isFixedTitle) {
            Utils.loge("WebActivity onPageFinished setWebTitle %s");
            setWebTitle(lWeb.webView.getTitle());
        }
        if (isSupportClose) {
            tClose.setVisibility(isRootPage ? View.GONE : View.VISIBLE);
        }
    }

    @Override
    public void onMenuAction(String type, String menuAction) {
        mRightMenuAction = menuAction;

        if (TextUtils.equals("image", type)) {
            if (TextUtils.equals("returnToHomeCard", mRightMenuAction)) {
                iMenu.setImageResource(R.mipmap.web_menu_card);
                iMenu.setVisibility(View.VISIBLE);
            }
        } else if (TextUtils.equals("text", type)) {
            if (TextUtils.equals("share", mRightMenuAction)) {
                tMenu.setText("分享");
                tMenu.setVisibility(View.VISIBLE);
            }
        } else if (TextUtils.equals("clear", type)) {
            iMenu.setVisibility(View.GONE);
            tMenu.setVisibility(View.GONE);
        }
    }

    @Override
    public void onAppExec(String cmd) {
        if (TextUtils.equals("forceBackFinish", cmd)) {
            isForceBackFinish = true;
        }
    }

    private void setWebTitle(String titleText) {
        Utils.loge("WebActivity setWebTitle %s", titleText);
        if (!TextUtils.isEmpty(titleText)) {
            if (titleText.startsWith("https://") || titleText.startsWith("http://")) {
                tTitle.setText("");
            } else if (titleText.length() > 10) {
                tTitle.setText(titleText.substring(0, 10) + "..");
            } else {
                tTitle.setText(titleText);
            }
        } else {
            tTitle.setText("");
        }
    }

    private void showShareDialog() {
        if (dlgShare == null) {
            dlgShare = new ShareDialog.Builder(this).setCallback(new ShareDialog.OnShareClickListener() {
                @Override
                public void onPlatformClick(Dialog dialog, SHARE_MEDIA platform) {
                    UMImage image = new UMImage(WebActivity.this, R.mipmap.ic_launcher);
                    String mShareTitle = lWeb.webView.getTitle();
                    String mShareContent = lWeb.webView.getUrl();
                    new ShareAction(WebActivity.this).setPlatform(platform)
                            .withText(mShareContent)
                            .withTitle(mShareTitle)
                            .withTargetUrl(mShareContent)
                            .withMedia(image)
                            .setCallback(new UMShareListener() {
                                @Override
                                public void onResult(SHARE_MEDIA share_media) {
                                    Utils.showToast("分享成功");
                                }

                                @Override
                                public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                                    Utils.showToast("分享失敗");
                                }

                                @Override
                                public void onCancel(SHARE_MEDIA share_media) {
                                    Utils.showToast("分享取消");
                                }
                            }).share();
                    dialog.dismiss();
                }
            }).create();
        }
        dlgShare.show();
    }
}
